package com.bana.sys.utils;

import java.io.DataInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class Convert {

    /**
     * 字节流转十六进制字符串
     *
     * @param buffer 字节流
     * @return 十六进制字符串
     */
    public static String toHex(byte[] buffer) {
        return toHex(buffer, " ");
    }

    /**
     * 字节流转十六进制字符串
     *
     * @param buffer    字节流
     * @param separator 分隔符
     * @return 十六进制字符串
     */
    public static String toHex(byte[] buffer, String separator) {
        if (buffer == null) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < buffer.length; ++i) {
            builder.append(String.format("%02x%s", buffer[i], separator));
        }
        return builder.toString();
    }


    public static byte xor(byte[] buffer, int start, int length) {
        byte b = 0;

        for (int i = start; i < length + start; ++i) {
            b ^= buffer[i];
        }
        return b;
    }

    public static byte[] hexToBuffer(String hex) {
        List<Byte> bytes = new ArrayList<>();
        for (String h : hex.split(" ")) {
            bytes.add((byte) Integer.parseInt(h, 16));
        }
        byte[] buffer = new byte[bytes.size()];
        for (int i = 0; i < bytes.size(); ++i) {
            buffer[i] = bytes.get(i);
        }
        return buffer;
    }

    public static byte[] toBuffer(String ip) {
        if (ip == null || ip.equals("")) {
            return new byte[4];
        }
        //先找到IP地址字符串中.的位置
        int position1 = ip.indexOf(".");
        int position2 = ip.indexOf(".", position1 + 1);
        int position3 = ip.indexOf(".", position2 + 1);
        //将每个.之间的字符串转换成整型
        byte[] buffer = new byte[4];
        buffer[3] = (byte) Integer.parseInt(ip.substring(0, position1));
        buffer[2] = (byte) Integer.parseInt(ip.substring(position1 + 1, position2));
        buffer[1] = (byte) Integer.parseInt(ip.substring(position2 + 1, position3));
        buffer[0] = (byte) Integer.parseInt(ip.substring(position3 + 1));
        return buffer;
    }

    public static String toIp(byte[] buffer, DataInputStream dis) {

        try {
            int start = buffer.length - dis.available();
            StringBuffer sb = new StringBuffer("");
            sb.append(Byte.toUnsignedInt(buffer[start + 3]));
            sb.append(".");
            sb.append(Byte.toUnsignedInt(buffer[start + 2]));
            sb.append(".");
            sb.append(Byte.toUnsignedInt(buffer[start + 1]));
            sb.append(".");
            sb.append(Byte.toUnsignedInt(buffer[start]));
            dis.skipBytes(4);
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String toString(byte[] buffer, DataInputStream dis) {

        try {
            int start = buffer.length - dis.available();
            int position = start;
            while (position < buffer.length) {
                if (buffer[position] == (byte) 0x00) {
                    byte[] ip = new byte[position - start];
                    dis.read(ip);
                    dis.skipBytes(1);
                    return new String(ip);
                }
                position++;
            }
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    public static int toByte(byte[] buffer, int index) {
        return Byte.toUnsignedInt(buffer[index]);
    }

    public static int toShort(byte[] buffer, int index) {
        int n = 0;
        n = (n | Byte.toUnsignedInt(buffer[index]));
        n = (n << 8);
        n = (n | Byte.toUnsignedInt(buffer[index + 1]));
        return n;
    }

    public static long toInt(byte[] buffer, int index) {
        int n = 0;
        n = (n | Byte.toUnsignedInt(buffer[index]));

        for (int i = 1; i < 4; ++i) {
            n = (n << 8);
            n = (n | Byte.toUnsignedInt(buffer[index + i]));
        }
        return n;
    }

    public static long toLong(byte[] buffer, int index) {
        long l = 0;
        l = (l | Byte.toUnsignedLong(buffer[index]));

        for (int i = 1; i < 8; ++i) {
            l = (l << 8);
            l = (l | Byte.toUnsignedLong(buffer[index + i]));
        }
        return l;
    }

    public static <T> String toString(List<T> values) {
        return toString(values, "", "");
    }

    public static <T> String toString(List<T> values, String start, String end) {
        if (values == null || values.isEmpty()) {
            return null;
        } else {
            StringBuilder builder = new StringBuilder();
            builder.append(start);
            for (T value : values) {
                builder.append(value);
                builder.append(",");
            }
            builder.delete(builder.length() - 1, builder.length());
            builder.append(end);
            return builder.toString();
        }
    }

    public static String toTimesm(long time) {
        if (time == 0L) {
            return "-";
        } else {
            long l = System.currentTimeMillis() - time;

            //一分钟之内
            if (l < 60000) {
                return String.format("%d秒之前", l / 1000);
            }
            //一小时之内
            else if (l >= 60000 && l < 3600000) {
                return String.format("%d分之前", l / 1000 / 60);
            }
            //大于一小时
            else {
                int hour = (int) Math.floor(l / 3600000);
                return String.format("%d小时", hour) + String.format("%d分之前", l / 1000 / 60 - hour * 60);
            }
        }
    }

}
